﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// The Administration page allows users to create new users to
/// assign to and create new issue categories.
/// </summary>
public partial class Administration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = string.Empty;
    }

    protected void btnNewUser_Click(object sender, EventArgs e)
    {
        string username = txtUname.Text;
        string fname = txtFname.Text;
        string lname = txtLname.Text;

        string connectionString =
                WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);

        using (con)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sprocInsertUser";

            try
            {
                cmd.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar, 20));
                cmd.Parameters["@username"].Value = username;

                cmd.Parameters.Add(new SqlParameter("@firstname", SqlDbType.VarChar, 50));
                cmd.Parameters["@firstname"].Value = fname;

                cmd.Parameters.Add(new SqlParameter("@lastname", SqlDbType.VarChar, 50));
                cmd.Parameters["@lastname"].Value = lname;

                con.Open();
                cmd.ExecuteNonQuery();
                lblMessage.Text = "User successfully created: " + fname + " " + lname;
            }
            catch
            {
                lblMessage.Text = "Error creating new user.";
            }
        }
    }

    protected void btnNewCategory_Click(object sender, EventArgs e)
    {
        string cat_name = txtNewCategory.Text;

        string connectionString =
                WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);

        using (con)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sprocInsertCategory";

            try
            {
                cmd.Parameters.Add(new SqlParameter("@cat_name", SqlDbType.VarChar, 50));
                cmd.Parameters["@cat_name"].Value = cat_name;

                con.Open();
                cmd.ExecuteNonQuery();
                lblMessage.Text = "Category successfully created: " + cat_name;
            }
            catch
            {
                lblMessage.Text = "Error creating new category.";
            }
        }
    }
}
